/**
 * Created by a1 on 2017/11/1.
 */
"use strict";
var fs = require('fs');
var formidable = require('formidable');
var common = require('../models/common.js');
var BookModel = require('../models/Book.js');//书本Schema
var BookreviewModel = require('../models/Bookreview.js');//书评Schema
var UserinfoModel = require('../models/Userinfo.js');//用户Schema
var UserreviewModel = require('../models/Userreview.js');//书评详情Schema

exports.forumKey=function(req,res){
    let session=false;
    if(req.session.username){
        session=true;
    }
    if(!isNaN(req.params.key)){
        res.render('forum',{
            page:'forum',
            bid:req.params.key,
            session:session,
            uid:req.session.uid?req.session.uid:""
        });
    }else {
        res.render('error',{
            page:'error'
        });
    }
};
exports.threadKey=function(req,res){
    let session=false;
    if(req.session.username){
        session=true;
    }
    if(!isNaN(req.params.key)){
        res.render('thread',{
            page:'thread',
            bid:req.params.key,
            session:session
        })
    }else {
        res.render('error',{
            page:'error'
        })
    }
};
exports.commentKey=function(req,res){
    let session=false;
    if(req.session.username){
        session=true;
    }
    if(!isNaN(req.params.key)){
        if(session){
            res.render('comment',{
                page:'comment',
                bid:req.params.key,
                session:session,
                uid:req.session.uid?req.session.uid:""
            })
        }else{
            res.render('login',{
                page:'login'
            })
        }
    }else {
        res.render('error',{
            page:'error'
        })
    }
};
exports.getforumList=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let bid=parseInt(fields.bid);
        let page=parseInt(fields.page);
        let pagenum=page*20;
        var keyTem=fields.key;
        console.log(keyTem);
        BookreviewModel.findLimitSkip({bid:bid},20,pagenum,{startTime:-1},function(err,data){
            if(err){
                res.json({"msg":'失败',"code":0,info:{}});
                return;
            }
            if(data==null||data==""){
                if(keyTem=="true"){
                    // console.log("没有评论");
                    res.json({"msg":'没有评论',"code":2,info:[]});
                }else{
                    res.json({"msg":'成功',"code":1,info:{data:[],userInfo:{}}});
                }
            }else{

                let arrimg=[];
                let arrnick=[];
                var obj={};
                (function bl(i){
                    i++;
                    if(i==data.length){
                        obj.data=data;
                        obj.userInfo={userimg:arrimg,usernick:arrnick};
                        // console.log("成功:"+JSON.stringify(obj));
                        res.json({"msg":'成功',"code":1,info:obj});
                        return;
                    }else{
                        UserinfoModel.findSortOne({"uid":data[i]['uid']},{"uid":1},function(err,docs){
                            //console.log(data1);
                            arrimg.push(docs['imgurl']);
                            arrnick.push(docs['nickname']);
                            bl(i);
                        });
                    }
                }(-1));
            }
        });
    });
};
exports.getforumCount=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let bid=parseInt(fields.bid);
        BookreviewModel.findConut({bid:bid},function(err,count){
            if(err){
                res.json({"msg":'失败',"code":0,info:{}});
                return;
            }
            console.log(count);
            res.json({"msg":'成功',"code":1,count:count});
        });
    });
};
exports.addForumCnt=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let bid=parseInt(fields.bid);
        let title=fields.title;
        let cnt=fields.cnt;
        if(req.session.uid){
            BookreviewModel.findSortOne({},{brid:-1},function(err,doc){
                if(err){
                    res.json({"msg":'失败',"code":0});
                    return;
                }
                if(doc==null){
                    let obj={
                        brid:1,
                        bid:bid,
                        uid:req.session.uid,
                        startTime:new Date().getTime(),
                        title:title,
                        cnt:cnt,
                        num:0,
                        hot:"F"
                    };
                    let data=new BookreviewModel(obj);
                    data.save(function(err){
                        BookModel.updateFn({bid:bid},{$set:{bookCount:1}},function(err,result){
                            res.json({"msg":'成功',"code":1});
                        });
                    });
                }else{
                    let obj={
                        brid:parseInt(doc.brid)+1,
                        bid:bid,
                        uid:req.session.uid,
                        startTime:new Date().getTime(),
                        title:title,
                        cnt:cnt,
                        num:0,
                        hot:"F"
                    };
                    let data=new BookreviewModel(obj);
                    data.save(function(err){
                        if(err){
                            res.json({"msg":'失败',"code":0});
                            return;
                        }
                        BookreviewModel.findConut({bid:bid},function(err,count){
                            BookModel.updateFn({bid:bid},{$set:{bookCount:count}},function(err,result){
                                res.json({"msg":'成功',"code":1});
                            });
                        });
                    });
                }
            });
        }else{
            res.json({"msg":'失败',"code":0});
        }
    });
};
exports.getforumHot=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let bid=parseInt(fields.bid);
        BookreviewModel.findConut({bid:bid,hot:"T"},function(err,count){
            if(err){
                res.json({"msg":'失败',"code":0,info:{}});
                return;
            }
            console.log(count);
            res.json({"msg":'成功',"code":1,count:count});
        });
    });
};
exports.getforumHotList=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let bid=parseInt(fields.bid);
        let page=parseInt(fields.page);
        let pagenum=page*10;
        var keyTem=fields.key;
        console.log(keyTem);
        BookreviewModel.findLimitSkip({bid:bid,hot:"T"},10,pagenum,{startTime:-1},function(err,data){
            if(err){
                res.json({"msg":'失败',"code":0,info:{}});
                return;
            }
            if(data==null||data==""){
                if(keyTem=="true"){
                    //console.log("没有评论");
                    res.json({"msg":'没有评论',"code":2,info:[]});
                }else{
                    res.json({"msg":'成功',"code":1,info:{data:[],userInfo:{}}});
                }
            }else{
                let arrimg=[];
                let arrnick=[];
                var obj={};
                (function bl(i){
                    i++;
                    if(i==data.length){
                        obj.data=data;
                        obj.userInfo={userimg:arrimg,usernick:arrnick};
                        // console.log("成功:"+JSON.stringify(obj));
                        res.json({"msg":'成功',"code":1,info:obj});
                        return;
                    }else{
                        UserinfoModel.findSortOne({"uid":data[i]['uid']},{"uid":1},function(err,docs){
                            //console.log(data1);
                            arrimg.push(docs['imgurl']);
                            arrnick.push(docs['nickname']);
                            bl(i);
                        });
                    }
                }(-1));
            }
        });
    });
};
exports.getForumInfo=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let bid=parseInt(fields.bid);
        let brid=parseInt(fields.brid);
        BookreviewModel.findSortOne({bid:bid,brid:brid},{brid:1},function(err,doc){
            if(err){
                res.json({"msg":'失败',"code":0,info:{}});
                return;
            }
            // console.log("doc"+JSON.stringify(doc));
            if(doc==null||doc==""){
                res.json({"msg":'没有这个评论',"code":2,info:{}});
            }else{
                UserinfoModel.findSortOne({"uid":doc['uid']},{"uid":1},function(err,doc1){
                    if(err){
                        res.json({"msg":'失败',"code":0,info:{}});
                        return;
                    }
                    // console.log("doc"+JSON.stringify(doc1));
                    res.json({"msg":'成功',"code":1,info:{brinfo:doc,urinfo:doc1}});
                });
            }

        });
    });
};
exports.getForumUserCount=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files) {
        let bid=parseInt(fields.bid);
        let brid=parseInt(fields.brid);
        UserreviewModel.findConut({bid:bid,brid:brid},function(err,count){
            if(err){
                res.json({"msg":'失败',"code":0,info:{}});
                return;
            }
            // console.log("doc"+JSON.stringify(doc1));
            res.json({"msg":'成功',"code":1,count:count});
        });
    });
};
exports.getForumUserList=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let bid=parseInt(fields.bid);
        let page=parseInt(fields.page);
        let brid=parseInt(fields.brid);
        let pagenum=page*10;
        var keyTem=fields.key;
        UserreviewModel.findLimitSkip({bid:bid,brid:brid},10,pagenum,{startTime:-1},function(err,data){
            if(err){
                res.json({"msg":'失败',"code":0,info:{}});
                return;
            }
            if(data==null||data==""){
                if(keyTem=="true"){
                    //console.log("没有评论");
                    res.json({"msg":'没有评论',"code":2,info:[]});
                }else{
                    res.json({"msg":'成功',"code":1,info:{data:[],userInfo:{}}});
                }
            }else{
                let arrimg=[];
                let arrnick=[];
                var obj={};
                (function bl(i){
                    i++;
                    if(i==data.length){
                        obj.data=data;
                        obj.userInfo={userimg:arrimg,usernick:arrnick};
                        // console.log("成功:"+JSON.stringify(obj));
                        res.json({"msg":'成功',"code":1,info:obj});
                        return;
                    }else{
                        UserinfoModel.findSortOne({"uid":data[i]['uid']},{"uid":1},function(err,docs){
                            //console.log(data1);
                            arrimg.push(docs['imgurl']);
                            arrnick.push(docs['nickname']);
                            bl(i);
                        });
                    }
                }(-1));
            }
        });
    });
};
exports.addForumUserCnt=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let bid=parseInt(fields.bid);
        let brid=parseInt(fields.brid);
        let cnt=fields.cnt;
        if(req.session.uid){
            UserreviewModel.findSortOne({},{urid:-1},function(err,doc){
                if(err){
                    res.json({"msg":'失败',"code":0});
                    return;
                }
                if(doc==null){
                    let obj={
                        urid:1,
                        bid:bid,
                        brid:brid,
                        uid:req.session.uid,
                        startTime:new Date().getTime(),
                        cnt:cnt
                    };
                    let data=new UserreviewModel(obj);
                    data.save(function(err){
                        BookreviewModel.updateFn({brid:brid},{$set:{num:1}},function(err,result){
                            res.json({"msg":'成功',"code":1});
                        })
                    });
                }else{
                    let obj={
                        urid:parseInt(doc.urid)+1,
                        brid:brid,
                        bid:bid,
                        uid:req.session.uid,
                        startTime:new Date().getTime(),
                        cnt:cnt
                    };
                    let data=new UserreviewModel(obj);
                    data.save(function(err){
                        if(err){
                            res.json({"msg":'失败',"code":0});
                            return;
                        }
                        UserreviewModel.findConut({brid:brid},function(err,count){
                            BookreviewModel.updateFn({brid:brid},{$set:{num:count}},function(err,result){
                                res.json({"msg":'成功',"code":1});
                            });
                        });
                    });
                }
            });
        }else{
            res.json({"msg":'失败',"code":0});
        }
    });
};